package com.cardbaobao.cardbabyclient.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.BaseLayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.ui.activity.CreditCardRaidersActivity;
import com.cardbaobao.cardbabyclient.ui.activity.FindBankActivity;
import com.cardbaobao.cardbabyclient.ui.activity.HotActivitiesActivity;
import com.cardbaobao.cardbabyclient.ui.activity.NoviceCreditCardActivity;
import com.cardbaobao.cardbabyclient.ui.activity.SpeedCardActivity;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.activity.web.EventWebViewActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.CooperationBankDelegateNewAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.CreditCardIndexBusinessAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.HighPrivilegeAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.HotRecDelegateAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.HotRecLabelDelegateAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.HotRecPopularRankingDelegateAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.HotRecQualityLifeDelegateAdapter;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.controller.CommonJumpController;
import com.cardbaobao.cardbabyclient.expand.transformer.ScaleTransformer;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.model.Banner;
import com.cardbaobao.cardbabyclient.model.BusinessClass;
import com.cardbaobao.cardbabyclient.model.CreditCardIndexLayout;
import com.cardbaobao.cardbabyclient.model.HeadCommon;
import com.cardbaobao.cardbabyclient.model.result.CreditCardIndexLayoutResult;
import com.cardbaobao.cardbabyclient.model.result.HeadCommonResult;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.utils.ViewUtil;
import com.cardbaobao.cardbabyclient.widget.RotateHeaderHandler;
import com.cardbaobao.cardbabyclient.widget.banner.ScalePagerView;
import com.cardbaobao.cardbabyclient.widget.banner.BannerHolderCreator;
import com.cardbaobao.cardbabyclient.widget.banner.BannerView;
import com.cardbaobao.cardbabyclient.widget.banner.BannerViewHolder;
import com.cardbaobao.cardbabyclient.widget.divider.VerticalItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by Jiangzx on 11:05.
 * 信用卡
 */

public class CreditFragment extends BaseFragment {
    private String prePageId;
    public void setBuriedInfo(String prePageId){
        this.prePageId=prePageId;
    }
    public static final int TYPE_LABEL = 1;
    public static final int TYPE_HOT_REC = 2;
    public static final int TYPE_COOPERATION_BANK = 3;
    public static final int TYPE_QUALITY_LIFE = 4;
    public static final int TYPE_POPULAR_RANKING = 5;

    @BindView(R.id.ptr_view)
    PtrFrameLayout ptrLayout;

    @BindView(R.id.main_appbar)
    AppBarLayout appBarLayout;

    @BindView(R.id.banner)
    BannerView<Banner.CarouselFigureListBean> bannerView;

    @BindView(R.id.recycler_view_top)
    RecyclerView recyclerViewTop;

//    @ViewInject(R.id.ll_marquee_container)
//    private LinearLayout llMarqueeContainer;
//    @ViewInject(R.id.marqueeView)
//    private MarqueeView marqueeView;

    @BindView(R.id.rbt_hot_rec)
    RadioButton rbtHotRec;

    @BindView(R.id.rbt_high_privilege)
    RadioButton rbtHighPrivilege;

    @BindView(R.id.view_divider)
    View divider;
    @BindView(R.id.radio_group)
    RadioGroup rgp;

    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private RecyclerView hotRecRecycleView;
    private DelegateAdapter hotRecAdapter;
    private List<DelegateAdapter.Adapter> adapters;

    private RecyclerView highPrivilegeRecycleView;

    private Class<?> toActivity = null;

    @Override
    protected void initViewFinish(View view, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        initIntent();

        mCurrentPageId= Self.Page.app_credit_card_index_page;

        ViewUtil.configBannerView(mApplicationContext, bannerView);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        hotRecRecycleView = new RecyclerView(mContext);
        hotRecRecycleView.setLayoutParams(params);
        final VirtualLayoutManager hotLayoutManager = new VirtualLayoutManager(mContext);
        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        viewPool.setMaxRecycledViews(TYPE_LABEL, 5);
        viewPool.setMaxRecycledViews(TYPE_HOT_REC, 1);
        viewPool.setMaxRecycledViews(TYPE_COOPERATION_BANK, 10);
        viewPool.setMaxRecycledViews(TYPE_QUALITY_LIFE, 4);
        hotRecRecycleView.setRecycledViewPool(viewPool);
        hotRecRecycleView.setLayoutManager(hotLayoutManager);
        hotRecAdapter = new DelegateAdapter(hotLayoutManager, false);
        adapters = new LinkedList<>();

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    ptrLayout.setEnabled(true);
                } else {
                    ptrLayout.setEnabled(false);
                }
                if(-verticalOffset==appBarLayout.getTotalScrollRange()){
                    divider.setVisibility(View.VISIBLE);
                }else{
                    divider.setVisibility(View.GONE);
                }
//                divider.setAlpha(-verticalOffset/(float)appBarLayout.getTotalScrollRange());
            }
        });

        hotRecRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (hotLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
                        appBarLayout.setExpanded(true, true);
                    }
                }
            }
        });

        final int size26 = DensityUtil.dp2px(13.0f);
        final int size32 = DensityUtil.dp2px(16.0f);

        highPrivilegeRecycleView = new RecyclerView(mContext);
        highPrivilegeRecycleView.setLayoutParams(params);
        highPrivilegeRecycleView.setPadding(size26, 0, size26, 0);
        highPrivilegeRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        highPrivilegeRecycleView.addItemDecoration(new VerticalItemDecoration(0, size32));
        highPrivilegeRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    LinearLayoutManager linearLayoutManager =
                            (LinearLayoutManager) highPrivilegeRecycleView.getLayoutManager();
                    if (linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
                        appBarLayout.setExpanded(true, true);
                    }
                }
            }
        });

        rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(checkedId==R.id.rbt_hot_rec){
                    //热门推荐
                    viewPager.setCurrentItem(0);
                }else{
                    //高端特权
                    viewPager.setCurrentItem(1);
                }
            }
        });

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                if (position == 0) {
                    container.addView(hotRecRecycleView);
                    return hotRecRecycleView;
                } else {
                    container.addView(highPrivilegeRecycleView);
                    return highPrivilegeRecycleView;
                }
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rbtHotRec.setChecked(true);
                    SelfUtils.buriedClickData(Self.Action.app_click_credit_card_hot_recommend,
                            Self.Action.app_click_credit_card_hot_recommend_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            prePageId);
                } else {
                    rbtHighPrivilege.setChecked(true);
                    SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_high_privilege,
                            Self.Action.app_click_credit_card_index_high_privilege_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            prePageId);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        final PtrUIHandler header = new RotateHeaderHandler(mContext);
        ptrLayout.setHeaderView((View) header);
        ptrLayout.addPtrUIHandler(header);
        ptrLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                bannerView.pause();
                loadHeaderData();//获取顶部轮播图数据
                loadRecycleViewData();//获取列表中的数据
            }
        });
        ptrLayout.disableWhenHorizontalMove(true);
        bannerView.setParent(ptrLayout);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ptrLayout.autoRefresh();
            }
        }, 200L);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 获取顶部公共数据
     */
    private void loadHeaderData() {
        Observable<String> observable= Api.getCommonHeader(Constant.ZCC0101);
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                ptrLayout.refreshComplete();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.showToast(ApplicationHelper.
                        getApplicationContext().
                        getString(R.string.string_request_server_fail));
            }

            @Override
            public void onNext(String s) {
                HeadCommonResult headCommonResult = JsonUtil.json2Object(s, HeadCommonResult.class);
                if (headCommonResult.getCode() == Constant.SUC_CODE) {
                    if (headCommonResult.getData() != null) {
                        //配置轮播图
                        if (headCommonResult.getData().getCarousel_figure_list() != null) {
                            setBannerUi(headCommonResult.getData().getCarousel_figure_list());
                        }
                        //配置业务分类
                        if (headCommonResult.getData().getTab_list() != null) {
                            setBusinessUi(headCommonResult.getData().getTab_list());
                        }
                        //配置跑马灯
//                        List<HeadCommon.MarqueeListBean> data =
//                                headCommonResult.getData().getMarquee_list();
//                        if (data != null && data.size() > 0) {
//                            setMarqueeUi(headCommonResult.getData().getMarquee_list());
//                        }
                    }
                } else {
                    ToastUtil.showToast(headCommonResult.getMsg());
                }
            }
        });
    }

    /**
     * 获取recycleView中的数据[热门推荐、合作银行、品质生活、人气排行、高端特权]
     */
    private void loadRecycleViewData() {
        Observable<String> observable=Api.getCreditCardIndexData();
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                ptrLayout.refreshComplete();
            }

            @Override
            public void onError(Throwable e) {
                ptrLayout.refreshComplete();
                ToastUtil.showToast(ApplicationHelper.
                        getApplicationContext().
                        getString(R.string.string_request_server_fail));
            }

            @Override
            public void onNext(String s) {
                CreditCardIndexLayoutResult layoutData =
                        JsonUtil.json2Object(s,CreditCardIndexLayoutResult.class);
                if (layoutData.getCode() == Constant.SUC_CODE) {
                    CreditCardIndexLayout data = layoutData.getData();
                    if (data != null) {
                        adapters.clear();
                        hotRecAdapter.clear();
                        hotRecAdapter.notifyDataSetChanged();
                        //热门推荐
                        if (data.getTab_manager_two() != null) {
                            setHotRecUi(data.getTab_manager_two());
                        }
                        //合作银行
                        if (data.getBankList() != null) {
                            setCooperationBankUi(data.getBankList());
                        }
                        //品质生活
                        if (data.getTab_manager_three() != null) {
                            setQualityLifeUi(data.getTab_manager_three());
                        }
                        //人气排行
                        if (data.getTab_manager_four() != null) {
                            setPopularRankingUi(data.getTab_manager_four());
                        }
                        //高端特权
                        if (data.getTab_manager_five() != null) {
                            List<CreditCardIndexLayout.HighPrivilege> privileges = data.getTab_manager_five();
                            //添加一个数据作为footer
                            privileges.add(new CreditCardIndexLayout.HighPrivilege());
                            setHighPrivilegeUi(privileges);

                        }
                        hotRecAdapter.setAdapters(adapters);
                        hotRecRecycleView.setAdapter(hotRecAdapter);
                    }
                } else {
                    ToastUtil.showToast(layoutData.getMsg());
                }
            }
        });
    }

    /**
     * 设置轮播图数据
     */
    private void setBannerUi(final List<Banner.CarouselFigureListBean> data) {
        bannerView.setPages(data, new BannerHolderCreator() {
            @Override
            public BannerViewHolder createViewHolder() {
                final ScalePagerView pagerView = new ScalePagerView();
                pagerView.setOnBannerItemClickedListener(new ScalePagerView.OnBannerItemClickedListener() {
                    @Override
                    public void onBannerItemClicked(Banner.CarouselFigureListBean bean) {
                        SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_top_banner,
                                Self.Action.app_click_credit_card_index_top_banner_des,
                                mEnterPageTime,
                                mCurrentPageId,
                                prePageId);
                        CommonJumpController controller = new CommonJumpController(mContext);
                        Map<String, Object> extra = new HashMap<>();
                        extra.put(CommonJumpController.FROM_PAGE, "信用卡首页-banner");
                        extra.put(EventWebViewActivity.WEB_TYPE_I, 11);
                        controller.bannerJump(bean, extra);
                    }
                });
                return pagerView;
            }
        }, new ScaleTransformer(0.8f));
        bannerView.start();
    }

    /**
     * 设置业务分类数据
     */
    private void setBusinessUi(final List<BusinessClass.TabListBean> data) {
        if (data.size() > 0) {
            final GridLayoutManager manager =
                    new GridLayoutManager(mContext, 2, GridLayoutManager.VERTICAL, false);
            manager.setAutoMeasureEnabled(true);
            manager.setSmoothScrollbarEnabled(true);
            recyclerViewTop.setHasFixedSize(true);
            recyclerViewTop.setNestedScrollingEnabled(false);
            recyclerViewTop.setLayoutManager(manager);
            final CreditCardIndexBusinessAdapter adapter =
                    new CreditCardIndexBusinessAdapter(mContext);
            adapter.setData(data);
            adapter.setClickedCallback(new RecycleItemClickedCallback() {
                @Override
                public void onRecycleItemClicked(int position) {
                    BusinessClass.TabListBean business =
                            data.get(position);
                    mIntent.putExtra(BaseActivity.PRE_PAGE_ID_S,mCurrentPageId);
                    switch (business.getCode()) {
                        case Constant.JBQ01060204:
                            //附近银行
                            SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_near_by_bank,
                                    Self.Action.app_click_credit_card_index_near_by_bank_des,
                                    mEnterPageTime,
                                    mCurrentPageId,
                                    prePageId);
                            mIntent.setClass(mContext, FindBankActivity.class);
                            startActivity(mIntent);
                            GoogleAnalyticsUtil.setScreenAction(Google.CAREGORY_ORD, "信用卡-" + business.getName());
                            break;
                        case Constant.JBQ01060203:
                            //热门活动
                            SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_hot_event,
                                    Self.Action.app_click_credit_card_index_hot_event_des,
                                    mEnterPageTime,
                                    mCurrentPageId,
                                    prePageId);
                            mIntent.setClass(mContext, HotActivitiesActivity.class);
                            startActivity(mIntent);
                            break;
                        case Constant.JBQ01060202:
                            //新手办卡
                            SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_novice_card,
                                    Self.Action.app_click_credit_card_index_novice_card_des,
                                    mEnterPageTime,
                                    mCurrentPageId,
                                    prePageId);
                            mIntent.setClass(mContext, NoviceCreditCardActivity.class);
                            startActivity(mIntent);
                            break;
                        case Constant.JBQ01060201:
                            //办卡攻略
                            SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_card_raiders,
                                    Self.Action.app_click_credit_card_index_card_raiders_des,
                                    mEnterPageTime,
                                    mCurrentPageId,
                                    prePageId);
                            mIntent.setClass(mContext, CreditCardRaidersActivity.class);
                            startActivity(mIntent);
                            break;
                    }
                }
            });
            recyclerViewTop.setAdapter(adapter);
        }
    }

    /**
     * 设置跑马灯数据
     */
    private void setMarqueeUi(final List<HeadCommon.MarqueeListBean> data) {
//        List<String> dataStr = new ArrayList<>();
//        for (int i = 0, size = data.size(); i < size; i++) {
//            dataStr.add(data.get(i).getContent());
//        }
//        marqueeView.setFlipInterval(Constant.MARQUEE_INTERVAL_TIME);
//        marqueeView.startWithList(dataStr);
//        marqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
//            @Override
//            public void onItemClick(int position, TextView textView) {
//                HeadCommon.MarqueeListBean bean = data.get(position);
//                CommonJumpController controller = new CommonJumpController(mContext);
//                Map<String, Object> extra = new HashMap<>();
//                extra.put(CommonJumpController.FROM_PAGE, "信用卡首页-跑马灯");
//                extra.put(EventWebViewActivity.WEB_TYPE_I, 12);
//                controller.marqueeJump(bean, extra);
//            }
//        });
//        llMarqueeContainer.setVisibility(View.VISIBLE);
    }

    /**
     * 设置热门推荐数据
     */
    private void setHotRecUi(final List<CreditCardIndexLayout.HotRec> data) {
        //热门推荐
        if (data.size() >= 3) {
            SingleLayoutHelper hotRecLayoutHelper = new SingleLayoutHelper();
//            hotRecLayoutHelper.setMarginTop(DensityUtil.dp2px(5));
            HotRecDelegateAdapter adapter = new HotRecDelegateAdapter(hotRecLayoutHelper, data);
            adapter.setRecycleItemClickedCallback(new RecycleItemClickedCallback() {
                @Override
                public void onRecycleItemClicked(int position) {
                    SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_hot_recommend_card,
                            Self.Action.app_click_credit_card_index_hot_recommend_card_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            prePageId);
                    CreditCardIndexLayout.HotRec bean = data.get(position);
                    CommonJumpController controller = new CommonJumpController(mContext);
                    if (bean.getPage_type() == Constant.TYPE_NATIVE) {
                        controller.nativeDetailJump(Constant.PAGE_ZCC0101, bean.getDetail_id(), bean.getDetail_name(), null);
                    } else {
                        Map<String, Object> map = new HashMap<>();
                        map.put(EventWebViewActivity.WEB_TYPE_I, 13);
                        controller.h5WebJump(bean.getPage_type(), bean.getUrl(), bean.getName(), map);
                    }
                }
            });
            adapters.add(adapter);
        }
    }

    /**
     * 设置合作银行
     */
    private void setCooperationBankUi(final List<CreditCardIndexLayout.CooperateBank> data) {
        if (data.size() > 0) {
            int margin26 = DensityUtil.dp2px(13.0f);
            int margin30 = DensityUtil.dp2px(15.0f);
            int padding42 = DensityUtil.dp2px(21.0f);
            int margin50 = DensityUtil.dp2px(25.0f);
            SingleLayoutHelper cooperationLabelLayoutHelper = new SingleLayoutHelper();
            cooperationLabelLayoutHelper.setMarginRight(margin26);
            cooperationLabelLayoutHelper.setMarginTop(margin50);
            adapters.add(new HotRecLabelDelegateAdapter(cooperationLabelLayoutHelper, "合作银行", 1));
            GridLayoutHelper cooperationLayoutHelper = new GridLayoutHelper(4);
            cooperationLayoutHelper.setMarginLeft(margin26);
            cooperationLayoutHelper.setMarginRight(margin26);
            cooperationLayoutHelper.setVGap(margin30);
            cooperationLayoutHelper.setPaddingTop(padding42);
            cooperationLayoutHelper.setPaddingBottom(padding42);
            cooperationLayoutHelper.setAutoExpand(false);
            cooperationLayoutHelper.setLayoutViewBindListener(new BaseLayoutHelper.LayoutViewBindListener() {
                @Override
                public void onBind(View layoutView, BaseLayoutHelper baseLayoutHelper) {
                    layoutView.setBackgroundResource(R.drawable.shape_corner_16px_solid_fff);
                }
            });
            CooperationBankDelegateNewAdapter adapter =
                    new CooperationBankDelegateNewAdapter(cooperationLayoutHelper, data);
            adapter.setRecycleItemClickedCallback(new RecycleItemClickedCallback() {
                @Override
                public void onRecycleItemClicked(int position) {
                    SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_cooperate_bank_item,
                            Self.Action.app_click_credit_card_index_cooperate_bank_item_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            prePageId);
                    CreditCardIndexLayout.CooperateBank bank = data.get(position);
                    mIntent.setClass(mContext, SpeedCardActivity.class);
                    mIntent.putExtra(SpeedCardActivity.BANK_ID_I, bank.getId());
                    mIntent.putExtra(SpeedCardActivity.BANK_NAME_S, bank.getName_short());
                    mIntent.putExtra(BaseActivity.PRE_PAGE_ID_S,mCurrentPageId);
                    startActivity(mIntent);
                }
            });
            adapters.add(adapter);
        }
    }

    /**
     * 设置品质生活数据
     */
    private void setQualityLifeUi(final List<CreditCardIndexLayout.QualityLife> data) {
        if (data.size() > 0) {
            int margin26 = DensityUtil.dp2px(13.0f);
            int margin50 = DensityUtil.dp2px(25.0f);
            int margin18 = DensityUtil.dp2px(9.0f);
            SingleLayoutHelper lifeLabelLayoutHelper = new SingleLayoutHelper();
            lifeLabelLayoutHelper.setMarginLeft(margin26);
            lifeLabelLayoutHelper.setMarginTop(margin50);
            lifeLabelLayoutHelper.setMarginBottom(margin18);
            adapters.add(new HotRecLabelDelegateAdapter(lifeLabelLayoutHelper, "品质生活", 0));
            GridLayoutHelper lifeLayoutHelper = new GridLayoutHelper(2);
            lifeLayoutHelper.setMarginLeft(margin26);
            lifeLayoutHelper.setMarginRight(margin26);
            lifeLayoutHelper.setVGap(margin18);
            lifeLayoutHelper.setHGap(margin18);
            lifeLayoutHelper.setAutoExpand(false);
            HotRecQualityLifeDelegateAdapter qualityLifeAdapter = new HotRecQualityLifeDelegateAdapter(lifeLayoutHelper, data);
            qualityLifeAdapter.setRecycleItemClickedCallback(new RecycleItemClickedCallback() {
                @Override
                public void onRecycleItemClicked(int position) {
                    SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_quality_life_item,
                            Self.Action.app_click_credit_card_index_quality_life_item_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            prePageId);
                    CreditCardIndexLayout.QualityLife qualityLife = data.get(position);
                    mIntent.setClass(mContext, SpeedCardActivity.class);
                    //主题
                    mIntent.putExtra(SpeedCardActivity.THEME_CODE_S, qualityLife.getCode());
                    mIntent.putExtra(SpeedCardActivity.THEME_NAME_S, qualityLife.getTheme());
                    mIntent.putExtra(BaseActivity.PRE_PAGE_ID_S,mCurrentPageId);
                    startActivity(mIntent);
                }
            });
            adapters.add(qualityLifeAdapter);
        }
    }

    /**
     * 设置人气排行数据
     */
    private void setPopularRankingUi(final List<CreditCardIndexLayout.PopularRanking> data) {
        if (data.size() > 0) {
            int margin50 = DensityUtil.dp2px(25.0f);
            int margin26 = DensityUtil.dp2px(13.0f);
            int margin18 = DensityUtil.dp2px(9.0f);
            SingleLayoutHelper popularLabelLayoutHelper = new SingleLayoutHelper();
            popularLabelLayoutHelper.setMarginTop(margin50);
            popularLabelLayoutHelper.setMarginRight(margin26);
            popularLabelLayoutHelper.setMarginBottom(margin18);
            adapters.add(new HotRecLabelDelegateAdapter(popularLabelLayoutHelper, "人气排行", 1));
            LinearLayoutHelper popularLayoutHelper = new LinearLayoutHelper();
            HotRecPopularRankingDelegateAdapter adapter =
                    new HotRecPopularRankingDelegateAdapter(popularLayoutHelper, data);
            adapter.setRecycleItemClickedCallback(new RecycleItemClickedCallback() {
                @Override
                public void onRecycleItemClicked(int position) {
                    SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_popularity_ranking,
                            Self.Action.app_click_credit_card_index_popularity_ranking_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            prePageId);
                    CreditCardIndexLayout.PopularRanking ranking = data.get(position);
                    CommonJumpController controller = new CommonJumpController(mContext);
                    if (ranking.getPage_type() == Constant.TYPE_NATIVE) {
                        controller.nativeDetailJump(Constant.PAGE_ZCC0101,
                                ranking.getDetail_id(),
                                ranking.getDetail_name(), null);
                    } else {
                        Map<String, Object> map = new HashMap<>();
                        map.put(EventWebViewActivity.WEB_TYPE_I, 14);
                        controller.h5WebJump(ranking.getPage_type(), ranking.getUrl(), ranking.getName(), null);
                    }
                }
            });
            adapters.add(adapter);
        }
    }

    /**
     * 设置高端特权数据
     */
    private void setHighPrivilegeUi(final List<CreditCardIndexLayout.HighPrivilege> data) {
        HighPrivilegeAdapter highPrivilegeAdapter = new HighPrivilegeAdapter(data);
        highPrivilegeAdapter.setClickedCallback(new RecycleItemClickedCallback() {
            @Override
            public void onRecycleItemClicked(int position) {
                SelfUtils.buriedClickData(Self.Action.app_click_credit_card_index_high_privilege_item,
                        Self.Action.app_click_credit_card_index_high_privilege_item_des,
                        mEnterPageTime,
                        mCurrentPageId,
                        prePageId);
                CreditCardIndexLayout.HighPrivilege privilege = data.get(position);
                CommonJumpController controller = new CommonJumpController(mContext);
                if (privilege.getPage_type() == Constant.TYPE_NATIVE) {
                    controller.nativeDetailJump(Constant.PAGE_ZCC0101,
                            privilege.getDetail_id(),
                            privilege.getDetail_name(), null);
                } else {
                    Map<String, Object> map = new HashMap<>();
                    map.put(EventWebViewActivity.WEB_TYPE_I, 15);
                    controller.h5WebJump(privilege.getPage_type(),
                            privilege.getUrl(),
                            privilege.getName(), null);
                }
            }
        });
        highPrivilegeRecycleView.setAdapter(highPrivilegeAdapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_index_credit;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
//            if (llMarqueeContainer.getVisibility() == View.VISIBLE) {
//                marqueeView.stopFlipping();
//            }
            if(bannerView!=null){
                bannerView.pause();
            }
        } else {
//            if (llMarqueeContainer.getVisibility() == View.VISIBLE) {
//                marqueeView.startFlipping();
//            }
            if(bannerView!=null){
                bannerView.start();
            }
            GoogleAnalyticsUtil.setScreenName(Google.NEW_CREDIT);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(com.cardbaobao.cardbabyclient.model.Event.LoginSucNotify loginSucNotify) {
        if (loginSucNotify.getType() == 1) {
            if (toActivity != null) {
                mIntent.setClass(mContext, toActivity);
                startActivity(mIntent);
                toActivity = null;
            }
        }
    }
}
